package com.future.hedongjun.mine.order;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.http.HttpRequest;
import com.future.baselib.view.IosAlertDialog;
import com.future.hedongjun.R;
import com.future.hedongjun.common.PriceHelper;
import com.future.hedongjun.mall.adapter.OrderDetailAdapter;
import com.future.hedongjun.mall.bean.OrderDetail;
import com.future.hedongjun.mine.bean.DeliveryAddressBean;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailActivity extends BaseActivity {

    @BindView(R.id.iv_tuikuan)
    ImageView ivTuikuan;
    @BindView(R.id.tv_tuikuan_type)
    TextView tvTuikuanType;
    @BindView(R.id.tv_tip_tuikuan_type)
    TextView tvTipTuikuanType;
    @BindView(R.id.tv_tuikuan_detail)
    TextView tvTuikuanDetail;
    @BindView(R.id.layout_tuikuan)
    RelativeLayout layoutTuikuan;
    @BindView(R.id.tv_tip_logistics_company)
    TextView tvTipLogisticsCompany;
    @BindView(R.id.tv_logistics_company)
    TextView tvLogisticsCompany;
    @BindView(R.id.tv_tip_logistics_num)
    TextView tvTipLogisticsNum;
    @BindView(R.id.tv_logistics_num)
    TextView tvLogisticsNum;
    @BindView(R.id.tv_look_logistics_detail)
    TextView tvLookLogisticsDetail;
    @BindView(R.id.layout_logistics)
    RelativeLayout layoutLogistics;
    @BindView(R.id.tv_close)
    TextView tvClose;
    @BindView(R.id.tv_custom_name)
    TextView tvCustomName;
    @BindView(R.id.tv_custom_phone)
    TextView tvCustomPhone;
    @BindView(R.id.tv_custom_address)
    TextView tvCustomAddress;
    @BindView(R.id.layout_shipping_address)
    RelativeLayout layoutShippingAddress;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_price_commodity)
    TextView tvPriceCommodity;
    @BindView(R.id.layout_price_commodity)
    FrameLayout layoutPriceCommodity;
    @BindView(R.id.tv_price_kd)
    TextView tvPriceKd;
    @BindView(R.id.layout_price_kd)
    FrameLayout layoutPriceKd;
    @BindView(R.id.tv_coupon_price)
    TextView tvCouponPrice;
    @BindView(R.id.tv_price_yf)
    TextView tvPriceYf;
    @BindView(R.id.layout_price_yf)
    FrameLayout layoutPriceYf;
    @BindView(R.id.tv_tip_order_num)
    TextView tvTipOrderNum;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_tip_order_time)
    TextView tvTipOrderTime;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.tv_tip_order_type)
    TextView tvTipOrderType;
    @BindView(R.id.tv_order_type)
    TextView tvOrderType;
    @BindView(R.id.tv_copy_order_num)
    TextView tvCopyOrderNum;
    @BindView(R.id.bottom_btn)
    Button bottomBtn;

    private String id;
    private OrderDetail orderDetail;
    private String order_num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        ButterKnife.bind(this);

        setTitle("订单详情");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        getData();
        orderDetail = new OrderDetail();
        orderDetail.addressBean = new DeliveryAddressBean();
        orderDetail.addressBean.setName("姓名");
        orderDetail.addressBean.setPhone("13999999999");
        orderDetail.addressBean.setLocation("浙江省杭州市滨江区垃圾街烤猪蹄");

        orderDetail.orderNum = "802934280136862162";
        orderDetail.orderTime = "8102/11/11 11:11:11";

        orderDetail.amount = 100;
        orderDetail.kdPrice = 5;
        orderDetail.couponPrice = 5;
        orderDetail.goodsBeanList = new ArrayList<>();
        orderDetail.goodsBeanList.add(new OrderDetail.GoodsBean());
        orderDetail.goodsBeanList.add(new OrderDetail.GoodsBean());
        switch (id) {
            case "1":
                orderDetail.status = 0;
                break;
            case "2":
                orderDetail.status = 1;
                break;
            case "3":
                orderDetail.status = 2;
                orderDetail.logisticsCompany = "顺丰快递";
                orderDetail.logisticsNum = "10000000000";
                break;
            case "4":
                orderDetail.status = 3;
                break;
            case "5":
                orderDetail.status = 4;
                break;
            case "6":
                orderDetail.status = 5;
                break;
            case "7":
                orderDetail.status = 6;
                break;
            case "8":
                orderDetail.status = 7;
                break;
        }
        initView(orderDetail);
//        updateViewVisible(0);
    }

    private void initView(OrderDetail orderDetail) {
        updateViewVisible(orderDetail.status);

        tvCustomName.setText(orderDetail.addressBean.getName());
        tvCustomPhone.setText(orderDetail.addressBean.getPhone());
        tvCustomAddress.setText( orderDetail.addressBean.getLocation());
        recyclerView.setAdapter(new OrderDetailAdapter(orderDetail.goodsBeanList));

        tvPriceCommodity.setText(PriceHelper.priceFormat(orderDetail.amount));
        tvPriceKd.setText(PriceHelper.priceFormat(orderDetail.kdPrice));
        tvCouponPrice.setText("-" + PriceHelper.priceFormat(orderDetail.couponPrice));
        tvPriceYf.setText(PriceHelper.priceFormat(orderDetail.amount));

        tvOrderNum.setText(orderDetail.orderNum);
        tvOrderTime.setText(orderDetail.orderTime);

        switch (orderDetail.status) {
            case 2:
                tvLogisticsCompany.setText(orderDetail.logisticsCompany);
                tvLogisticsNum.setText(orderDetail.logisticsNum);
                break;
        }

    }

    private void getData() {
        new HttpRequest<>()
                .with(this)
                .addParam("order_num", order_num)
                .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                    }
                }).start(new DefaultResponse());
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        order_num = extras.getString("order_num");
    }

    private void updateViewVisible(int type) {
        layoutLogistics.setVisibility(View.GONE);
        layoutTuikuan.setVisibility(View.GONE);
        switch (type) {
            case 0://待支付
                tvOrderType.setText("待支付");
                tvClose.setVisibility(View.VISIBLE);
                tvClose.setText("取消订单");
                bottomBtn.setVisibility(View.VISIBLE);
                bottomBtn.setText("立即支付");
                break;
            case 1://待发货
                tvOrderType.setText("待发货");
                tvClose.setVisibility(View.VISIBLE);
                tvClose.setText("申请退款");
                bottomBtn.setVisibility(View.GONE);
                break;
            case 2://待收货
                tvOrderType.setText("待收货");
                layoutLogistics.setVisibility(View.VISIBLE);
                tvClose.setVisibility(View.VISIBLE);
                tvClose.setText("申请退款");
                bottomBtn.setVisibility(View.VISIBLE);
                bottomBtn.setText("确认收货");
                break;
            case 3://待评价
                tvOrderType.setText("待评价");
                tvClose.setVisibility(View.GONE);
                bottomBtn.setVisibility(View.VISIBLE);
                bottomBtn.setText("立即评价");
                break;
            case 4://已完成
                tvOrderType.setText("已完成");
                tvClose.setVisibility(View.GONE);
                bottomBtn.setVisibility(View.GONE);
                break;
            case 5://已取消
                tvOrderType.setText("已取消");
                tvClose.setVisibility(View.GONE);
                bottomBtn.setVisibility(View.GONE);
                bottomBtn.setVisibility(View.GONE);
                break;
            case 6://退款审核中
                tvOrderType.setText("退款审核中");
                ivTuikuan.setImageResource(R.drawable.tuikuan_shenhe);
                tvTipTuikuanType.setText("商家将及时与您联系退款事宜");
                tvTuikuanType.setText("退款审核中");
                layoutTuikuan.setVisibility(View.VISIBLE);
                bottomBtn.setVisibility(View.GONE);
                tvClose.setVisibility(View.VISIBLE);
                tvClose.setText("取消退款");
                break;
            case 7://退款成功
                tvOrderType.setText("退款成功");
                layoutTuikuan.setVisibility(View.VISIBLE);
                ivTuikuan.setImageResource(R.drawable.tuikuan_chenggong);
                tvTipTuikuanType.setText("退款金额已经原路返回到您的支付账户");
                tvTuikuanType.setText("退款成功");
                tvClose.setVisibility(View.GONE);
                bottomBtn.setVisibility(View.GONE);
                break;
        }
    }

    @OnClick({R.id.tv_tuikuan_detail, R.id.tv_look_logistics_detail, R.id.tv_close, R.id.tv_copy_order_num, R.id.bottom_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_tuikuan_detail://退款详情
                Bundle bundle1 = new Bundle();
                bundle1.putString("id", orderDetail.id);
                bundle1.putSerializable("data", orderDetail);
                bundle1.putInt("type", RefundActivity.TYPE_REFUND_DETAIL);
                startActivity(RefundActivity.class, bundle1);
                break;
            case R.id.tv_look_logistics_detail://物流详情
                Bundle bundle = new Bundle();
                bundle.putString("logisticsNum", orderDetail.logisticsNum);
                startActivity(LogisticsDetailActivity.class, bundle);
                break;
            case R.id.tv_close:
                switch (orderDetail.status) {
                    case 0://取消订单
                        new IosAlertDialog(this)
                                .builder()
                                .setMsg("确定要取消订单吗?")
                                .setPositiveButton("确定", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                    }
                                })
                                .setNegativeButton("取消", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                    }
                                }).show();
                        break;
                    case 1:
                    case 2://申请退款
                        Bundle bundle2 = new Bundle();
                        bundle2.putString("id", orderDetail.id);
                        bundle2.putSerializable("data", orderDetail);
                        bundle2.putInt("type", RefundActivity.TYPE_REFUND);
                        startActivity(RefundActivity.class, bundle2);
                        break;
                    case 6://取消退款
                        new IosAlertDialog(this)
                                .builder()
                                .setMsg("确定要取消退款吗?")
                                .setPositiveButton("确定", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                    }
                                })
                                .setNegativeButton("取消", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                    }
                                }).show();
                        break;
                }
                break;
            case R.id.tv_copy_order_num:
                String orderNum = tvOrderNum.getText().toString().trim();
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cm.setText(orderNum);
                toast.show("复制成功");
                break;
            case R.id.bottom_btn:
                switch (orderDetail.status) {
                    case 0://立即支付

                        break;
                    case 2://确认收货

                        break;
                    case 3://立即评价
                        Bundle bundle2 = new Bundle();
                        bundle2.putSerializable("data",orderDetail);
                        startActivity(OrderEvaluateActivity.class, bundle2);
                        break;
                }
                break;
        }
    }
}
